<?php
// $Id: UserRightModel.class.php,v 1.2 2006/10/26 09:44:12 altair Exp $

include_once('BaseModel.class.php');
class UserRightModel extends BaseModel
{
    function get_priv($privilege)
    {
        $sql  = "SELECT module_name as 'priv' 
                FROM `user_rights`
                JOIN user_modules ON user_modules.modules_id=user_rights.modules_id
                LEFT JOIN usertype ON user_rights.usertype_id=usertype.id
                WHERE user_rights.active=1 AND LOWER(usertype.usertype_name) LIKE  LOWER('$privilege%')";
        return $this->query($sql, true);
            
    }

    function getlist ($usertype)
    {
		$query = "SELECT user_modules.*, user_right_id, active, usertype_id
					FROM `user_rights` 
					JOIN user_modules ON user_modules.modules_id=user_rights.modules_id
					WHERE usertype_id = '$usertype' AND usertype_id <> ''";	
	    if($usertype) $this->addmodule($usertype);
        return $this->query($query, true);
    }


    function usertypelist ()
    {
        $query = "SELECT id,usertype_name as 'name' from usertype WHERE active='1'";
        return $this->query($query, true);
    }
	
    function moduletypelist ()
    {
        $query = "SELECT DISTINCT(type) from user_modules";
        return $this->query($query, true);
    }
	
	function addmodule($id)
	{
			$query = "INSERT INTO user_rights (usertype_id, modules_id) 
						(SELECT '$id',modules_id FROM user_modules WHERE modules_id NOT IN 
						(SELECT modules_id FROM user_rights WHERE usertype_id='$id'));";
        return $this->query($query, false);
	}


    function clearlist($id)
    {
			
        $query = "UPDATE user_rights SET active=0 WHERE usertype_id='$id'";
        return $this->query($query, false);
										
    }

    function updateme($id)
	{
        $query = "UPDATE user_rights SET active=1 WHERE user_right_id=$id";
        return $this->query($query, false);
    }
}

?>
